//
//  HGMineHeader.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/7/31.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGMineHeaderView.h"
#import "HJCornerRadius.h"

static CGFloat margin = 15;

@interface HGMineHeaderView()

@property (strong, nonatomic) UIView *orderBgView;

@property (strong, nonatomic) UIImageView *user_Icon;

@property (strong, nonatomic) UIButton *typeButton;
@end

@implementation HGMineHeaderView

- (instancetype)init {
    if (self = [super init]) {
     
        [self initSubViews];
    }
    return self;
}


- (void)setCertificationType:(NSString *)certificationType {
    _certificationType = certificationType;
    
    [_typeButton setTitle:certificationType forState:(UIControlStateNormal)];
}

- (void)initSubViews {
    self.backgroundColor = kColorBackground;
    
    CGFloat iconWH = 65 * kScale;
    
    UIImageView *bgimgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wd-bg"]];
    [self addSubview:bgimgView];

    [bgimgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
    }];
    
    _user_Icon = [[UIImageView alloc] init];
    _user_Icon.aliCornerRadius = iconWH/2;
    [_user_Icon sd_setImageWithURL:kPhotoURL(kQQUserInfoManger.avatarUrl) placeholderImage:[UIImage imageNamed:@"mrtx"] options:(SDWebImageProgressiveDownload)];
    [bgimgView addSubview:_user_Icon];
    
    [_user_Icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kNavigationHeight);
        make.left.offset(margin);
        make.size.mas_equalTo(CGSizeMake(iconWH, iconWH));
    }];
    
    _user_Name = [[UILabel alloc] init];
    _user_Name.text = @"昵称";
    _user_Name.textColor = [UIColor whiteColor];
    [self addSubview:_user_Name];
    
    [_user_Name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.user_Icon).offset(margin);
        make.left.equalTo(self.user_Icon.mas_right).offset(margin);
    }];
    
    _user_level = [[UILabel alloc] init];
    _user_level.textColor = [UIColor whiteColor];
    [self addSubview:_user_level];
    
    [_user_level mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.user_Name.mas_bottom).offset(5);
        make.left.equalTo(self.user_Name);
    }];
    
    UIButton *bgButton = [[UIButton alloc] init];
    bgButton.tag = 5000;
    [bgButton addTarget:self action:@selector(actionCilk:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:bgButton];
    
    [bgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.bottom.top.equalTo(_user_Icon);
        make.width.equalTo(self).multipliedBy(0.5);
    }];
    
    [self.orderBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth - margin * 2, 143));
        make.centerX.equalTo(self);
        make.bottom.offset(-margin);
    }];
    
    
    _typeButton = [[UIButton alloc] init];
    _typeButton.backgroundColor = kColorRGBAlpha(225, 225, 225, 0.5);
    _typeButton.layer.cornerRadius = 11;
    _typeButton.tag = 1000;
    [_typeButton setTitleColor:kColorWithFloat(0x662e29) forState:(UIControlStateNormal)];
    [_typeButton setTitle:@"未认证" forState:(UIControlStateNormal)];
    _typeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_typeButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    [_typeButton addTarget:self action:@selector(actionCilk:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:_typeButton];
    
    [_typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_user_Icon);
        make.right.offset(-margin);
        make.size.mas_equalTo(CGSizeMake(60, 22));
    }];
    
}

- (UIView *)orderBgView {
    if (!_orderBgView) {
        _orderBgView = [[UIView alloc] init];
        _orderBgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_orderBgView];
        
        UIView *oneView = [[UIView alloc] init];
        [_orderBgView addSubview:oneView];

        [oneView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.orderBgView);
            make.height.offset(47);
        }];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = kColorBackground;
        [oneView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(oneView);
            make.height.offset(1);
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"我的订单";
        label.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:label];

        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(oneView);
            make.left.offset(margin);
        }];

        UIButton *orderBtn = [[UIButton alloc] init];
        [orderBtn setTitle:@"查看全部订单" forState:(UIControlStateNormal)];
        [orderBtn setTitleColor:kColorWithFloat(0x999999) forState:(UIControlStateNormal)];
        orderBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [orderBtn setImage:[UIImage imageNamed:@"right_more"] forState:(UIControlStateNormal)];
        [orderBtn addTarget:self action:@selector(actionCilk:) forControlEvents:(UIControlEventTouchUpInside)];
        [orderBtn layoutButtonWithEdgeInsetsStyle:(LGButtonEdgeInsetsStyleRight) imageTitleSpace:10];
        [oneView addSubview:orderBtn];

        [orderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(label);
            make.right.offset(-margin);
        }];

        UIView *settingView = [[UIView alloc] init];
        [_orderBgView addSubview:settingView];

        [settingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(oneView.mas_bottom);
            make.left.right.bottom.equalTo(self.orderBgView);
        }];
        
        NSArray *titles = @[@"待付款",@"待发货",@"待收货",@"交易完成"];
        for (int i = 0; i < 4; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.tag = i + 1;
            [button setImage:[UIImage imageNamed:titles[i]] forState:(UIControlStateNormal)];
            [button setTitle:titles[i] forState:(UIControlStateNormal)];
            [button setTitleColor:kColorText forState:(UIControlStateNormal)];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            [button addTarget:self action:@selector(actionCilk:) forControlEvents:(UIControlEventTouchUpInside)];
            [settingView addSubview:button];
        }

        [settingView.subviews mas_distributeViewsAlongAxis:(MASAxisTypeHorizontal) withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
        [settingView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(settingView);
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            for (UIButton *button in settingView.subviews) {
                [button layoutButtonWithEdgeInsetsStyle:(LGButtonEdgeInsetsStyleTop) imageTitleSpace:15];
            }
        });
    
    }
    return _orderBgView;
}

- (void)actionCilk:(UIButton *)button {
    
    if (button.tag == 5000) {
        if (_delegate && [_delegate respondsToSelector:@selector(pushMineProfile)]) {
            [_delegate pushMineProfile];
        }
        return;
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(seletedType:)]) {
        [_delegate seletedType:button.tag];
    }
}

@end
